// pages/newAddress/newAddress.js
const AV = getApp().globalData.AV
Page({

    /**
     * 页面的初始数据
     */
    data: {
        openids: '', //openid
        names: '', //收货人名
        provinceaddress: ['山东省', '济南市', '历下区'], //省地址
        detailaddress: '', //详细地址
        phones: '', //电话
        doors: '', //门牌号
        objectid: '' //编辑地址传过来的id
    },
    
    // 省地址的改变
    bindRegion(e) {
        this.setData({
            provinceaddress: e.detail.value
        })
    },
    // 表单提交事件
    formSubmit(e) {
        // console.log(e.detail.value);
        this.setData({
            names: e.detail.value.names,
            phones: e.detail.value.phones,
            detailaddress: e.detail.value.detailaddres,
            doors: e.detail.value.doors
        })
        if (this.data.objectid) {
            const todo = AV.Object.createWithoutData("address", this.data.objectid);
            todo.set("name", this.data.names);
            todo.set("openId", this.data.openids);
            todo.set("provinceAddress", this.data.provinceaddress);
            todo.set("phone", this.data.phones);
            todo.set("door", this.data.doors);
            todo.set("detailAddress", e.detail.value.detailaddress);
            todo.save();
            wx.showToast({
                title: '保存成功',
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 1,
                })
            }, 2000);
        } else {
            // 将数据存入leancloud的表中
            const Todo = AV.Object.extend('address');
            const todo = new Todo();
            todo.set("name", this.data.names);
            todo.set("openId", this.data.openids);
            todo.set("provinceAddress", this.data.provinceaddress);
            todo.set("phone", this.data.phones);
            todo.set("door", this.data.doors);
            todo.set("detailAddress", e.detail.value.detailaddress);
            //  将对象保存到云端
            todo.save().then(
                (todo) => {
                    // 成功保存之后，执行其他逻辑
                    console.log(`保存成功objectId：${todo.id}`);
                    wx.showToast({
                        title: '保存成功',
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1,
                        })
                    }, 2000);
                }, (error) => {
                    // 异常处理
                    console.log(error + '异常处理');
                }
            )
        }
    },
    // // 删除地址
        delete() {
        const todo = AV.Object.createWithoutData("address", this.data.objectid);
        todo.destroy();
        wx.showToast({
            title: '删除成功',
        })
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 2000);

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.nameIndex);
        // console.log(options.openids);
        this.setData({
            openids: options.openids,
            objectid: options.objectid

        })
        if (options.nameIndex == 1) {
            wx.setNavigationBarTitle({
                title: '新增地址',
            })
        } else {
            wx.setNavigationBarTitle({
                title: '编辑地址',
            })
            const query = new AV.Query('address');
            query.equalTo('objectId', this.data.objectid)
            query.find().then((res) => {
                console.log(res);
                this.setData({
                    names: res[0].attributes.name,
                    phones: res[0].attributes.phone,
                    provinceaddress: res[0].attributes.provinceAddress,
                    detailaddress: res[0].attributes.detailAddress,
                    doors: res[0].attributes.door
                })
            })

        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})